<template>
  <div class="app-container">
    <div class="box">
      <div class="box_content">
        <el-select v-model="LessName" placeholder="请选择学期" value="" filterable allow-create default-first-option>
          <el-option v-for="(item,index) in querySemester" :key="index" :label="item.semester_name" :value="item.semester_name" />
        </el-select>
      </div>
      <div class="box_content">
        <el-date-picker
          v-model="startTime"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="date"
          placeholder="选择开始日期"
        />
      </div>
      <div class="box_content">
        <el-date-picker
          v-model="endTime"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="date"
          placeholder="选择结束日期"
        />
      </div>
      <div class="box_content">
        <el-select v-model="defTime" placeholder="选择默认日期" value="">
          <el-option v-for="(item,index) in queryTableData" :key="index" :label="item.timetable_name" :value="item.unid">{{ item.timetable_name }}</el-option>
        </el-select>
      </div>
      <div class="box_content" style="text-align: right">
        <el-button type="primary" @click="queryScreenSemesterData">查询</el-button>
        <el-button @click="restLess">查看全部学期</el-button>
      </div>
    </div>
    <el-button type="primary" style="margin-right: 10px;margin-bottom: 10px;margin-top: 10px" @click="dialog1 = true">新增
    </el-button>说明：【起始日期】必须是每周一，【结束日期】必须是每周日。
    <el-table v-loading="loading" :data="queryData" border style="width: 100%" height="845">
      <el-table-column type="index" label="序号" width="55" align="center" prop="index" />
      <el-table-column align="center" prop="semester_name" label="学期名称" :show-overflow-tooltip="true" />
      <el-table-column align="center" prop="start_time" label="起始日期" :show-overflow-tooltip="true" />
      <el-table-column align="center" prop="end_time" label="结束日期" :show-overflow-tooltip="true" />
      <el-table-column align="center" prop="timetable_name" label="作息时间" :show-overflow-tooltip="true" />
      <el-table-column align="center" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-search" @click="handleClick(scope.row)">查看</el-button>
          <el-button type="success" size="small" icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="新增学期" :visible.sync="dialog1" width="300px">
      <el-form ref="addData" :model="addData" :rules="rules" label-width="80px">
        <el-form-item label="学期名称" prop="semester_name">
          <el-input v-model="addData.semester_name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="起始日期" prop="start_time">
          <el-date-picker
            v-model="addData.start_time"
            type="date"
            placeholder="选择日期"
            style="width: 100%;"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="结束日期" prop="end_time">
          <el-date-picker
            v-model="addData.end_time"
            type="date"
            placeholder="选择日期"
            style="width: 100%;"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="作息时间" prop="timetable_id">
          <el-select v-model="addData.timetable_id" placeholder="请选择">
            <el-option v-for="(item,index) in queryTableData" :key="index" :label="item.timetable_name" :value="item.unid">{{ item.timetable_name }}</el-option>
          </el-select>
        </el-form-item>
        <div style="text-align: center">
          <el-button type="primary" @click="addSemester('addData')">提交</el-button>
        </div>
      </el-form>
    </el-dialog>
    <el-dialog title="编辑" :visible.sync="editDig" width="300px">
      <el-form ref="editData" :model="editData" :rules="rules" label-width="80px">
        <el-form-item label="学期名称" prop="semester_name">
          <el-input v-model="editData.semester_name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="起始日期" prop="start_time">
          <el-date-picker
            v-model="editData.start_time"
            type="date"
            placeholder="选择日期"
            style="width: 100%;"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="结束日期" prop="end_time">
          <el-date-picker
            v-model="editData.end_time"
            type="date"
            placeholder="选择日期"
            style="width: 100%;"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="作息时间" prop="timetable_id">
          <el-select v-model="editData.timetable_id" placeholder="请选择">
            <el-option v-for="(item,index) in queryTableData" :key="index" :label="item.timetable_name" :value="item.unid">{{ item.timetable_name }}</el-option>
          </el-select>
        </el-form-item>
        <div style="text-align: center">
          <el-button type="primary" @click="editD">修改</el-button>
        </div>
      </el-form>
    </el-dialog>
    <el-dialog title="查看学期" :visible.sync="dialog3" width="600px">
      <el-form>
        <el-row>
          <el-col :span="12">
            <el-form-item label="学期名称" label-width="90px">
              <el-input v-model="querySem.semester_name" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="作息时间" label-width="90px">
              <el-input v-model="querySem.timetable_name" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="起始日期" label-width="90px">
              <el-input v-model="querySem.start_time" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" label-width="90px">
              <el-input v-model="querySem.end_time" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建日期" label-width="90px">
              <el-input v-model="querySem.create_time" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人id" label-width="90px">
              <el-input v-model="querySem.createor_id" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人" label-width="90px">
              <el-input v-model="querySem.createor_name" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机构id" label-width="90px">
              <el-input v-model="querySem.institution_id" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编辑日期" label-width="90px">
              <el-input v-model="querySem.update_time" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
    addSemester,
    queryTimetable,
    querySemesterData,
    querySemesterList,
    queryLessionList
} from '@/api/studentRecruit.js'
export default {
    name: 'Index2',
    data() {
        return {
            semesterRow: null,
            defTime: null,
            startTime: null,
            endTime: null,
            LessName: null,
            querySemester: null,
            queryLession: null,
            loading: null,
            requestIdObj: {},
            dialog1: false,
            editDig: false,
            dialog3: false,
            tableData: [{
                date: '2016-05-02',
                name: '王小虎',
                province: '上海',
                city: '普陀区',
                address: '上海市普陀区金沙江路 1518 弄',
                zip: 200333
            }],
            // 查看
            querySem: {
                semester_name: null,
                timetable_name: null,
                start_time: null,
                end_time: null,
                create_time: null,
                createor_id: null,
                createor_name: null,
                institution_id: null,
                update_time: null
            },
            // 编辑
            editData: {
                semester_name: null,
                start_time: null,
                end_time: null,
                timetable_id: null
            },
            // 添加
            addData: {
                semester_name: null,
                start_time: null,
                end_time: null,
                timetable_id: null
            },
            queryTableData: null,
            queryData: null,
            rules: {
                semester_name: [{
                    required: true,
                    message: '请输入名称',
                    trigger: 'blur,change'
                }],
                start_time: [{
                    required: true,
                    message: '请输入排序',
                    trigger: 'blur,change'
                }],
                end_time: [{
                    required: true,
                    message: '请输入机构id',
                    trigger: 'blur,change'
                }],
                timetable_id: [{
                    required: true,
                    message: '请选择栏目id',
                    trigger: 'blur,change'
                }]
            }
        }
    },
    computed: {},
    created() {
        this.querySemesterData()
        this.queryTimetable()
        this.querySemesterList()
        this.queryLessionList()
    },
    methods: {
        // 编辑
        edit(row) {
            this.editDig = true
            console.log(row)
            this.editData = {
                unid: row.unid,
                semester_name: row.semester_name,
                start_time: row.start_time,
                end_time: row.end_time,
                timetable_id: row.timetable_id
            }
        },
        // 编辑数据
        editD() {
            this.editDig = false
            console.log(this.editData)
            addSemester(this.editData).then(rs => {
                if (rs.code == 200) {
                    console.log(rs)
                    this.$message.success('编辑成功！')
                    this.querySemesterData()
                    this.$forceUpdate()
                } else {
                    this.$message.success('编辑失败！')
                }
            })
        },
        // 获取学期信息
        async querySemesterList() {
            let queryData = {}
            let resData = await querySemesterList(queryData)
            this.querySemester = resData.data
            console.log('获取学期信息', resData.data)
        },
        // 获取课程信息
        async queryLessionList() {
            let queryData = {}
            let resData = await queryLessionList(queryData)
            this.queryLession = resData.data
            console.log('获取课程信息', resData.data)
        },
        // 重置条件查询
        async restLess() {
            this.LessName = null
            this.defTime = null
            this.startTime = null
            this.endTime = null
            this.$message.success('重置成功！')
            this.querySemesterData()
        },
        // 条件查询
        async queryScreenSemesterData() {
            console.log(this.LessName)
            console.log(this.startTime)
            console.log(this.endTime)
            console.log(this.defTime)
            if (this.LessName == null && this.defTime == null && this.startTime == null && this.endTime == null) {
                this.$message.error('请输入要查询的条件！')
            } else {
                let queryData = {
                    semester_name: this.LessName,
                    timetable_id: this.defTime,
                    start_time: this.startTime,
                    end_time: this.endTime
                }
                let resData = await querySemesterData(queryData)
                if (resData.code == 200) {
                    this.$message.success('查询成功！')
                    this.queryData = resData.data
                    console.log(resData)
                } else {
                    this.$message.error('查询失败！')
                }
            }
        },
        handleClick(row) {
            console.log(row)
            this.dialog3 = true
            this.querySem = row
        },
        // 查询学期表数据
        async querySemesterData() {
            this.loading = true
            let queryData = {}
            let resData = await querySemesterData(queryData)
            if (resData.code == 200) {
                console.log('学期设置：', resData)
                this.queryData = resData.data
                // this.$message.success('查询成功！')
                this.loading = false
            } else {
                this.$message.error('查询失败！')
            }
        },
        async queryTimetable() {
            let queryData = {}
            let resData = await queryTimetable(queryData)
            this.queryTableData = resData.data
        },
        // 添加学期数据
        addSemester(addData) {
            this.$refs[addData].validate((valid) => {
                if (valid) {
                    let beginTime = new Date(this.addData.start_time)
                    console.log(beginTime)
                    let endTime = new Date(this.addData.end_time)
                    console.log(beginTime)
                    if (+endTime <= +beginTime) {
                        console.log('pppppppppppppp')
                        this.$message.error('结束日期必须大于起始日期')
                    } else if (+endTime > +beginTime) {
                        console.log('----------------------')
                        addSemester(this.addData).then(resData => {
                            console.log(resData, 'ppppppppppppppp')
                            if (resData.code == 200) {
                                console.log('addData', this.addData)
                                this.$message.success('添加成功')
                                this.dialog1 = false
                                this.$refs[addData].resetFields()
                                this.querySemesterData()
                            } else {
                                this.$message.success('添加失败')
                            }
                        })
                    }
                } else {
                    console.log(valid)
                    return false
                }
            })
        }
    }
}
</script>
<style scoped>
  .box {
    width: 100%;
    display: flex;
    flex-direction: row;
    /*//两侧对齐*/
    justify-content: space-between;
    /*//自动换行*/
    flex-wrap: wrap;
    /*//清除上下行的中间空隙*/
    align-content: flex-start;
  }
  .box_content {
    flex: 30 200px;
  }
  /*//解决最后一行的问题*/
  .box:after {
    content: '';
    width: 30%;
  }
</style>
